package view.wizard;

import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.Displayable;

import view.MVCComponent;

/**
 * Extends MVCComponent to add wizard commands and validation methods
 * 
 * Based on the article "Big designs for small devices"
 * 
 * @author Marcelo Gatto
 */
public abstract class WizardView extends MVCComponent {

	public final static int DIR_FORWARD  = +5;
	public final static int DIR_BACKWARD = -5;

	public final static int OK     = +1;
	public final static int REJECT = -1;

	public final static Command NEXT_COMMAND = new Command("NEXT", Command.OK, 1);
	public final static Command BACK_COMMAND = new Command("BACK", Command.BACK, 1);

	private WizardEngine wzdEngine;

	public void setEngine(WizardEngine engine) {
		wzdEngine = engine;
	}
	
	public void showScreen() {
		super.showScreen();
		if(!wzdEngine.isLast(this))  getScreen().addCommand(NEXT_COMMAND);
		if(!wzdEngine.isFirst(this)) getScreen().addCommand(BACK_COMMAND);
	}

	// MVCComponent methods to manage your view and model
	public abstract Displayable getScreen();
	protected abstract void createView();
	protected abstract void initModel();
	protected abstract void updateView();
	
	// Validation methods
	public abstract int onEnter(int direction);
	public abstract int onLeave(int direction);
	
	protected void errorMsg(String title, String msg) {
		Alert alert = new Alert(title, msg, null, AlertType.ERROR);
		alert.setTimeout(Alert.FOREVER);
		display.setCurrent(alert);
	}
	
	public void commandAction(Command cmd, Displayable display) {
		if(cmd==NEXT_COMMAND || cmd==BACK_COMMAND)
			wzdEngine.wizardCmdAction(cmd, this);
		else viewCommandAction(cmd, display);
	}

	// The new command action for your view commnads, except the wizard ones (next,back)
	protected abstract void viewCommandAction(Command cmd, Displayable display);
	
}